﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Cosoware.Web.Navigation
{
    public partial class CalendarSiteMapProvider
    {
        /// <summary>
        /// Nodes by keys
        /// </summary>
        private Dictionary<String, SiteMapNode> _nodes = null;

        /// <summary>
        /// Parents by it's child keys
        /// </summary>
        private Dictionary<String, SiteMapNode> _parentNodes = null;

        /// <summary>
        /// Childs by it's parent
        /// </summary>
        private Dictionary<SiteMapNode, List<SiteMapNode>> _childNodes = null;

        /// <summary>
        /// Loads the site map from dates.
        /// </summary>
        private void CreateSiteMap()
        {
            _rootNode = null;
            _nodes = new Dictionary<String, SiteMapNode>();
            _parentNodes = new Dictionary<String, SiteMapNode>();
            _childNodes = new Dictionary<SiteMapNode, List<SiteMapNode>>();
            DateTime beginDate = new DateTime(2007, 1, 1);
            DateTime endDate = DateTime.Now;
            CreateRootSubTree(CreateNode(null, _urlBase, _urlBase, Title), beginDate.Year, endDate.Year);
        }

        /// <summary>
        /// Creates the node.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="key">The key.</param>
        /// <param name="url">The URL.</param>
        /// <param name="title">The title.</param>
        /// <returns></returns>
        private SiteMapNode CreateNode(SiteMapNode parent, String key, String url, String title)
        {
            SiteMapNode node = new SiteMapNode(this, key, url, title);
            _nodes.Add(node.Key, node);
            if (_rootNode == null)
            {
                _rootNode = node;
            }
            if (parent != null)
            {
                _parentNodes.Add(key, parent);
                if (!_childNodes.ContainsKey(parent))
                    _childNodes[parent] = new List<SiteMapNode>();
                _childNodes[parent].Add(node);
                //parent.ChildNodes.Add(node);
            }
            return node;
        }

        /// <summary>
        /// Creates the root sub tree.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="beginYear">The begin year.</param>
        /// <param name="endYear">The end year.</param>
        private void CreateRootSubTree(SiteMapNode node, int beginYear, int endYear)
        {
            for (int year = beginYear; year <= endYear; year++)
            {
                DateTime dt = new DateTime(year, 1, 1);
                String url = GetUrl(dt.ToString("yyyy"));
                String title = dt.ToString("yyyy");
                SiteMapNode child = CreateNode(node, url, url, title);
                CreateYearSubTree(child, dt.Year);
            }
        }

        /// <summary>
        /// Creates the year sub tree.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="year">The year.</param>
        private void CreateYearSubTree(SiteMapNode node, int year)
        {
            for (int month = 1; month <= 12; month++)
            {
                DateTime dt = new DateTime(year, month, 1);
                String url = GetUrl(dt.ToString("yyyyMM"));
                String title = dt.ToString("MMMM");
                SiteMapNode child = CreateNode(node, url, url, title);
                CreateMonthSubTree(child, year, month);
            }
        }

        /// <summary>
        /// Creates the month sub tree.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="year">The year.</param>
        /// <param name="month">The month.</param>
        private void CreateMonthSubTree(SiteMapNode node, int year, int month)
        {
            for (int day = 1; day <= DateTime.DaysInMonth(year, month); day++)
            {
                DateTime dt = new DateTime(year, month, day);
                String url = GetUrl(dt.ToString("yyyyMMdd"));
                String title = dt.Day.ToString();
                SiteMapNode child = CreateNode(node, url, url, title);
            }
        }

        /// <summary>
        /// Gets the url.
        /// </summary>
        /// <param name="rawDate">The date (or part of date) represented in string.</param>
        /// <returns></returns>
        private string GetUrl(string rawDate)
        {
            return String.Format(_urlPattern, rawDate);
        }
    }
}
